package com.qiku.controller;

import com.github.pagehelper.PageInfo;
import com.qiku.domin.Menu;
import com.qiku.domin.MenuVo;
import com.qiku.domin.ResponseResult;
import com.qiku.service.MenuService;
import com.qiku.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @Autowired
    private RoleService roleService;


    /**
     * 回显菜单信息 包括父级菜单信息
     * @return
     */
    @GetMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(@RequestParam Integer id){
        if (id == -1){
            // 添加新菜单的操作  不需要查询菜单信息
            List<Menu> menuList = menuService.findSubMenuListByPid(id);
            Map<String , Object> map = new HashMap<>();
            map.put("menuInfo" , null);
            map.put("parentMenuList" , menuList);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        }else {
            // 修改操作  回显当前菜单可以选择的上级菜单
             Menu menu = menuService.findMenuById(id); // 当前菜单（要修改的）
            List<Menu> menuList = menuService.findSubMenuListByPid(-1);
            Map<String , Object> map = new HashMap<>();
            map.put("menuInfo" , menu);
            map.put("parentMenuList" , menuList);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        }
    }





    @PostMapping("/saveOrUpdateMenu")
    public ResponseResult saveOrUpdateMenu(@RequestBody Menu menu){
        if (null != menu.getId() && 0 < menu.getId()){
            menuService.UpdateMenu(menu);
            ResponseResult result = new ResponseResult(true, 200, "修改成功", null);
            return result;
        }else {
            menuService.saveMenu(menu);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", null);
            return result;
        }
    }



    @GetMapping("/findAllMenu")
    public ResponseResult findAllMenu(){
        List<Menu> menuList = roleService.fingAllMenu();
        Map<String , Object> map = new HashMap<>();
        map.put("list",menuList);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
        return result;
    }



}
